package cn.highset.modules.base.serviceimpl;

import cn.highset.common.constant.CommonConstant;
import cn.highset.modules.base.dao.DictDataDao;
import cn.highset.modules.base.entity.Dict;
import cn.highset.modules.base.entity.DictData;
import cn.highset.modules.base.service.DictDataService;
import cn.highset.modules.base.service.DictService;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 字典数据接口实现
 *
 * @author Victor
 */
@Slf4j
@Service
@Transactional
public class DictDataServiceImpl implements DictDataService {

    @Autowired
    private DictDataDao dictDataDao;
    @Autowired
    private DictService dictService;

    @Override
    public DictDataDao getRepository() {
        return dictDataDao;
    }

    @Override
    public Page<DictData> findByCondition(DictData dictData, Pageable pageable) {

        return dictDataDao.findAll(new Specification<DictData>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<DictData> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {

                Path<String> titleField = root.get("title");
                Path<Integer> statusField = root.get("status");
                Path<String> dictIdField = root.get("dictId");

                List<Predicate> list = new ArrayList<Predicate>();

                //模糊搜素
                if (StrUtil.isNotBlank(dictData.getTitle())) {
                    list.add(cb.like(titleField, '%' + dictData.getTitle() + '%'));
                }

                //状态
                if (dictData.getStatus() != null) {
                    list.add(cb.equal(statusField, dictData.getStatus()));
                }

                //所属字典
                if (StrUtil.isNotBlank(dictData.getDictId())) {
                    list.add(cb.equal(dictIdField, dictData.getDictId()));
                }

                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        }, pageable);
    }

    @Override
    public List<DictData> findByDictId(String dictId) {

        return dictDataDao.findByDictIdAndStatusOrderBySortOrder(dictId, CommonConstant.STATUS_NORMAL);
    }

    @Override
    public void deleteByDictId(String dictId) {

        dictDataDao.deleteByDictId(dictId);
    }

    /**
     * 通过dictId 与 value值获取字典数据
     *
     * @param dictId
     * @param value
     * @return
     */
    @Override
    public DictData findByDictIdAndValue(String dictId, String value) {
        List<DictData> list = dictDataDao.findByDictIdAndValue(dictId, value);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 通过dictId 与 title值获取字典数据
     *
     * @param dictId
     * @param title
     * @return
     */
    @Override
    public DictData findByDictIdAndTitle(String dictId, String title) {
        List<DictData> list = dictDataDao.findByDictIdAndTitle(dictId, title);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 通过字典类型  与 Value  获取字典数据
     *
     * @param dictType
     * @param value
     * @return
     */
    @Override
    public String findByDictTypeAndValue(String dictType, String value) {
        if (StrUtil.isBlank(dictType) || StrUtil.isBlank(value)) {
            return "";
        }
        Dict dict = dictService.findByType(dictType);
        if (dict != null) {
            DictData dictData = findByDictIdAndValue(dict.getId(), value);
            if (dictData != null) {
                return dictData.getTitle();
            }
        }
        return "";
    }

    /**
     * 通过字典类型  与 Title  获取字典数据
     *
     * @param dictType
     * @param title
     * @return
     */
    @Override
    public String findByDictTypeAndTitle(String dictType, String title) {
        if (StrUtil.isBlank(dictType) || StrUtil.isBlank(title)) {
            return "";
        }
        Dict dict = dictService.findByType(dictType);
        if (dict != null) {
            DictData dictData = findByDictIdAndTitle(dict.getId(), title);
            if (dictData != null) {
                return dictData.getValue();
            }
        }
        return "";
    }

}
